package com.management.department.teacherinfosystem.config;

import com.management.department.teacherinfosystem.Entity.StudentGuidance;
import com.management.department.teacherinfosystem.Entity.StudentGuidanceAchievement;
import com.management.department.teacherinfosystem.Entity.User;
import com.management.department.teacherinfosystem.Repository.StudentGuidanceRepository;
import com.management.department.teacherinfosystem.Repository.UserRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.ArrayList;

@Component
@RequiredArgsConstructor
public class StudentGuidanceDataInitializer implements ApplicationRunner {

    private final StudentGuidanceRepository guidanceRepository;
    private final UserRepository userRepository;

    @Override
    @Transactional
    public void run(ApplicationArguments args) throws Exception {
        // 若已有数据，避免重复插入过多；这里简单保证至少有3条示例
        if (guidanceRepository.count() >= 3) return;

        // 找到示例用户（ActivityDataInitializer 已创建 t001/张三）
        User teacher = userRepository.findById(2L).orElse(null);
        if (teacher == null) return; // 未找到ID=2（张三），跳过

        // 记录1：李华 本科生 毕业论文
        StudentGuidance g1 = new StudentGuidance();
        g1.setUser(teacher);
        g1.setStudentName("李华");
        g1.setStudentType("本科生");
        g1.setGuidanceCategory("毕业论文");
        g1.setGuidanceTopic("基于机器学习的文本分类研究");
        g1.setStartDate(LocalDate.of(2024,3,1));
        g1.setEndDate(LocalDate.of(2024,6,30));
        g1.setAchievements(new ArrayList<>());
        StudentGuidanceAchievement a11 = new StudentGuidanceAchievement();
        a11.setGuidance(g1);
        a11.setAchievementType("优秀毕业论文");
        a11.setTitle("《基于机器学习的文本分类研究》被评为校级优秀毕业论文");
        a11.setLevel("校级");
        a11.setAchievementDate(LocalDate.of(2024,7,10));
        g1.getAchievements().add(a11);
        guidanceRepository.save(g1);

        // 记录2：王强 本科生 学科竞赛
        StudentGuidance g2 = new StudentGuidance();
        g2.setUser(teacher);
        g2.setStudentName("王强");
        g2.setStudentType("本科生");
        g2.setGuidanceCategory("学科竞赛");
        g2.setGuidanceTopic("全国大学生数学建模竞赛训练");
        g2.setStartDate(LocalDate.of(2024,5,1));
        g2.setEndDate(LocalDate.of(2024,9,15));
        g2.setAchievements(new ArrayList<>());
        StudentGuidanceAchievement a21 = new StudentGuidanceAchievement();
        a21.setGuidance(g2);
        a21.setAchievementType("获奖");
        a21.setTitle("数学建模竞赛一等奖");
        a21.setLevel("省级一等奖");
        a21.setAchievementDate(LocalDate.of(2024,10,5));
        g2.getAchievements().add(a21);
        guidanceRepository.save(g2);

        // 记录3：赵敏 硕士生 科研训练
        StudentGuidance g3 = new StudentGuidance();
        g3.setUser(teacher);
        g3.setStudentName("赵敏");
        g3.setStudentType("硕士生");
        g3.setGuidanceCategory("科研训练");
        g3.setGuidanceTopic("自然语言处理方向：实体识别与关系抽取");
        g3.setStartDate(LocalDate.of(2024,1,10));
        g3.setEndDate(LocalDate.of(2024,12,20));
        g3.setAchievements(new ArrayList<>());
        StudentGuidanceAchievement a31 = new StudentGuidanceAchievement();
        a31.setGuidance(g3);
        a31.setAchievementType("论文");
        a31.setTitle("基于对比学习的实体关系抽取方法");
        a31.setLevel("CSSCI");
        a31.setAchievementDate(LocalDate.of(2024,11,18));
        g3.getAchievements().add(a31);
        StudentGuidanceAchievement a32 = new StudentGuidanceAchievement();
        a32.setGuidance(g3);
        a32.setAchievementType("项目");
        a32.setTitle("省级大学生创新训练项目立项");
        a32.setLevel("省级立项");
        a32.setAchievementDate(LocalDate.of(2024,6,1));
        g3.getAchievements().add(a32);
        guidanceRepository.save(g3);
    }
} 